﻿
@{
    Layout = "~/Views/_LayoutMain.cshtml";
}
<link href="~/scripts/webuploader/webuploader.css" rel="stylesheet" />
<script src="~/scripts/webuploader/webuploader.js"></script>
<style>
    #tblprogress {
        width: 100%;
        bottom: 0;
        left: 0;
        height: 8px;
        overflow: hidden;
        z-index: 50;
        margin: 0;
        border-radius: 0;
        background: none;
        -webkit-box-shadow: 0 0 0;
    }

        #tblprogress span {
            display: inline-block;
            overflow: hidden;
            width: 0;
            height: 8px;
            background: #1483d8 url('/Content/Images/progress.png') repeat-x;
            -webit-transition: width 200ms linear;
            -moz-transition: width 200ms linear;
            -o-transition: width 200ms linear;
            -ms-transition: width 200ms linear;
            transition: width 200ms linear;
            -webkit-animation: progressmove 2s linear infinite;
            -moz-animation: progressmove 2s linear infinite;
            -o-animation: progressmove 2s linear infinite;
            -ms-animation: progressmove 2s linear infinite;
            animation: progressmove 2s linear infinite;
            -webkit-transform: translateZ(0);
        }
</style>
<p id="tblprogress"><span></span></p>
<div class="easyui-panel" title="我的文件" style="padding:6px;" id="filepanel">
    <table id="dg" style="height:500px;"></table>

    <div id="mm" class="easyui-menu" style="width:120px;" data-options="onClick:menuHandler">
        <div data-options="name:'open'">打开</div>
        <div data-options="name:'refresh'">刷新</div>
        <div class="menu-sep"></div>
        <div data-options="name:'copy'">复制</div>
        <div data-options="name:'paste'">粘贴</div>
        <div data-options="name:'delete'">删除</div>
        <div class="menu-sep"></div>
        <div data-options="name:'newfolder'">新建文件夹</div>
        <div data-options="name:'upload'">上传</div>
    </div>

    <div id="tb" style="padding:5px;height:auto">
        <div>
            搜索: <input id="tbkey" class="easyui-textbox" style="width:120px">
            <a href="#" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
            <a id="filePicker" class="easyui-linkbutton">上传文件</a>
        </div>
        <div id="fileList" class="uploader-list" style="padding:5px;"></div>
    </div>
</div>
<script>
    var GUID = WebUploader.Base.guid();
    $(function () {
        $('#dg').height(window.innerHeight - 110).datagrid({
            url: 'GetFileSystem?uId=34BF10BB-7017-4995-B64E-19E418C470FB',
            idField: 'ID',
            toolbar: '#tb',
            singleSelect: true,
            columns: [[
                { field: 'ID', title: 'ID', hidden: true },
                {
                    field: 'Title', title: '名称', width: 120, formatter: function (value, row, index) {
                        if (row.IsFolder)
                            return '<span class="tree-icon tree-folder "></span>' + value;
                        else
                            return value;
                    }
                },
                { field: 'DateUpdatedString', title: '修改时间', width: 120 },
                {
                    field: 'IsFolder', title: '类型', width: 100, formatter: function (value, row, index) {
                        if (value)
                            return '文件夹';
                    }
                },
                {
                    field: 'FileSize', title: '大小', width: 100, align: 'right', formatter: function (value, row, index) {
                        if (value == 0)
                            return '';
                        else
                            return value;
                    }
                }
            ]]
        });

        //$(document).bind('contextmenu', function (e) {
        //    e.preventDefault();
        //    $('#mm').menu('show', {
        //        left: e.pageX,
        //        top: e.pageY
        //    });
        //});

        // 初始化Web Uploader
        var uploader = WebUploader.create({
            dnd: '#filepanel',
            disableGlobalDnd: true,
            // 选完文件后，是否自动上传。
            auto: false,
            chunked: true,//开始分片上传
            formData: {
                guid: GUID //自定义参数，待会儿解释
            },
            fileNumLimit: 1,
            fileSingleSizeLimit: 1024 * 1024 * 500,
            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false,
            // swf文件路径
            swf: '/scripts/webuploader/Uploader.swf',
            // 文件接收服务端。
            server: '/Default/Upload',
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: '#filePicker'
        });
        $list = $('#fileList');
        uploader.on('fileQueued', function (file) {
            if (file.size > 1024 * 1024 * 500) {
                $.messager.alert('提示', '文件大小不能超过500M！', 'info');
                return false;
            }
            var existsfile = false;
            uploader.md5File(file).then(function (val) {
                $.get('/Default/CheckFile', { hashcode: val, userid: $('#userid').val() }, function (data) {
                    if (data.code == 200) {
                        existsfile = true;
                    }
                }, 'json');
            });
            if (existsfile) {
                $.messager.alert('提示', '文件已经存在！', 'info');
                return false;
            }
            var $li = $(
                    '<div id="' + file.id + '" class="file-item">' +
                         '<div class="info">' + file.name + '</div></div>'
                    );

            // $list为容器jQuery实例
            $list.append($li);
        });



        // 文件上传过程中创建进度条实时显示。
        uploader.on('uploadProgress', function (file, percentage) {
            $percent = $('#tblprogress').find('span');
            $percent.css('width', percentage * 100 + '%');
        });

        // 文件上传成功，给item添加成功class, 用样式标记上传成功。
        uploader.on('uploadSuccess', function (file) {
            $('#' + file.id).addClass('upload-state-done');
            $.post('/Default/Merge', { guid: GUID, fileName: file.name, folderid: '' }, function (data) {
                $('#dg').datagrid('reload');
            });
        });

        // 文件上传失败，显示上传出错。
        uploader.on('uploadError', function (file) {
            var $li = $('#' + file.id),
                $error = $li.find('div.error');

            // 避免重复创建
            if (!$error.length) {
                $error = $('<div class="error"></div>').appendTo($li);
            }

            $error.text('上传失败');
        });

        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on('uploadComplete', function (file) {
            $('#tblprogress').css('width', '0%');
        });
    });

    function menuHandler(item) {
        if (item.name == 'upload') {

        }
    }
</script>
